﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using EHTWebsiteProject.Code.Entity;


namespace EHTWebsiteProject
{
    /// <summary>
    /// chiennv
    /// page: products
    /// </summary>
    public partial class Products : System.Web.UI.Page
    {
        private Int32 DanhMucID = HangSo.DEFAULT_ID;
        private Int32 pageIndex = 1;
        private Int32 pageSize = 20;
        private Int32 totalRecord;     

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    DanhMucID = Int32.Parse(Request.QueryString["DanhMucID"].ToString());
                    Session["DanhMucID"] = DanhMucID.ToString();
                }
                catch
                {
                    DanhMucID = HangSo.DEFAULT_ID;
                }

                try
                {
                    pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
                }
                catch (Exception ex)
                {
                    pageIndex = 1;
                }

                if (DanhMucID != HangSo.DEFAULT_ID)
                    loadSanPham(DanhMucID);                

            }
        }

        public void loadSanPham(Int32 DanhMucID)
        {
            DataSet ds = SanPhamProvider.GetSanPhamByDanhMucID(DanhMucID, pageIndex, pageSize, out totalRecord);
            if(ds.Tables[0].Rows.Count>0)
            {
                lbCategoryname.Text = DanhMucSanPhamProvider.GetDanhMucSanPhamByDanhMucID(DanhMucID).TenDanhMucVN;

                dtlProductList.DataSource = ds;
                dtlProductList.DataBind();

                this.PaggingControl1.PageURL = "Produkter-"+DanhMucID+".html";
                this.PaggingControl1.TotalRecord = totalRecord;
                this.PaggingControl1.PagingSize = pageSize;
            }
            
        }


        protected void ImgBuy_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            string SanPhamID = btn.CommandArgument;
            try
            {
                for (int i = 0; i < dtlProductList.Items.Count; i++)
                {
                    TextBox proID = (TextBox)(dtlProductList.Items[i].FindControl("txtProID"));
                    if (proID.Text == SanPhamID.ToString())
                    {
                        TextBox txtquatity = (TextBox)(dtlProductList.Items[i].FindControl("txtquantity"));

                        int quantity = Int32.Parse(txtquatity.Text.ToString());
                        if (quantity == 0)
                        {
                            ltrmessage.Text = "Vennligst skriv inn mengde";
                            Response.Redirect("Produkter-" + Session["DanhMucID"].ToString() + ".html");
                        }
                        else
                        {

                            ShoppingCart.Instance.AddItem(Int32.Parse(SanPhamID.ToString()), quantity);
                            Response.Redirect("Produkter-" + Session["DanhMucID"].ToString() + ".html");
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                ltrmessage.Text = "Erorr: Antall er ikke numberic";
            }

        }

        protected void dtlProductList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            Label lbImagProduct = (Label)e.Item.FindControl("lbImagProduct");
            Image ImagProduct = (Image)e.Item.FindControl("ImagProduct");
            if (lbImagProduct != null)
                ImagProduct.ImageUrl = lbImagProduct.Text;
            else
                ImagProduct.ImageUrl = "~/ImageDatas/Products/noImage.gif";
        }

        protected void dtlProductList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}